package models;

import java.io.Serializable;

import play.cache.Cache;
import play.data.validation.Required;
import siena.Id;
import siena.Model;
import siena.Query;

/**
 * @author oleksandr.bezhan
 * 
 *         Apr 7, 2012
 */
public class Product extends Model implements Serializable {
    public static Query<Product> all() {
        return Model.all(Product.class);
    }

    public int calories;

    @Id
    public Long id;

    @Required
    public String name;

    public Product() {
        super();
    }

    public Product(String name) {
        super();
        this.name = name;
    }

    /**
     * Cache product for 12h
     */
    @Override
    public void get() {
        String key = "Product:" + this.id;
        Product product = (Product) Cache.get(key);
        if (product == null) {
            super.get();
            Cache.set(key, this, "12h");
        } else {
            this.calories = product.calories;
            this.name = product.name;
        }
    }

    @Override
    public String toString() {
        return this.name;
    }
}
